//index.js
const app = getApp()

Page({
  data: {
    lists: [],
    ranks: [],
    selectedTownIndex: null,
    month: ''
  },
  onLoad: function() {
    this.getTime()
    this.getSelect()
    this.getRank()
  },

  // 分享页面
  onShareAppMessage: function () {
    return {
      title: '随时随地查晋城房价',
    }
  },

  getTime () {
    let a = new Date().getMonth()
    if (a === 0) {
      a + 1
    }
    this.setData({
      month: a
    })
  },

  // 下拉搜索跳转详情页面
  selected(e) {
    if (e.detail.value) {
      this.setData({
        selectedTownIndex: e.detail.value
      })
    }
  },

  search() {
    const id = this.data.lists[this.data.selectedTownIndex].id
    const url = `/pages/search/search?id=${id}`
    wx.navigateTo({
      url
    })
  },

  // 获取最新排行榜
  getRank() {
    let that = this
    wx.request({
      url: 'https://jc.loupan.com/index.php?c=fangjia&m=get_price_top&cityarea_id=&_=1631156569777',
      success: function(res) {
        res.data.data.map((item, index) => {
          item.created_at = item.created_at.slice(0, 10)
          item.index = index + 1
        })
        that.setData({
          ranks: res.data.data
        })
      }
    })
  },

  // 获取下拉信息
  getSelect() {
    let that = this
    wx.request({
      url: 'https://jc.loupan.com/index.php?c=fangjia&m=get_price_top&cityarea_id=&_=1631080287759', //仅为示例，并非真实的接口地址
      success: function(res) {
        that.setData({
          lists: res.data.data
        })
      }
    })
  }
})
